package com.jt.www.aop;

import com.jt.www.annotations.AddHeader;
import config.FeignConfiguration;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * 添加请求头
 * Created by shaoyn on 2020/05/13.
 */
@Aspect
@Component
public class AddHeaderAspect {

    public static final Logger log = LoggerFactory.getLogger(AddHeaderAspect.class);

    /**
     * 获取注解中对方法的描述信息
     *
     * @param joinPoint 切点
     * @return 方法描述
     * @throws Exception
     */
    private static AddHeader getMethodDescription(JoinPoint joinPoint)
            throws Exception {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        AddHeader addHeader = method.getAnnotation(AddHeader.class);
        return addHeader;
    }

    @Before("@annotation(com.jt.www.annotations.AddHeader)")
    public void addHeader(JoinPoint joinPoint) throws Throwable {
        AddHeader addHeader = getMethodDescription(joinPoint);
        FeignConfiguration.addHeader(addHeader.headerName(), addHeader.value());
    }

}
